package utilities.module.definedmods;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.Properties;

import utilities.manager.definederunner.GameRunner;
import utilities.module.Module;

public class GameScreen extends Module
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -9184362513900568045L;
	
	protected GameRunner runner;
	protected Color bgColor;
	
	protected Module mousedOver = null;
	
	/**
	 * 
	 * @param container
	 */
	public GameScreen(GameRunner runner) 
	{
		super(0, 0, runner != null ? runner.getWidth() : 0, runner != null ? runner.getHeight() : 0);
		this.runner = runner;
		bgColor = Color.white;
	}
	
	/*******************************************************************
	 * 
	 * INHERITED METHODS
	 * 
	 *******************************************************************/

	/**
	 * 
	 */
	public void update(Properties globals) 
	{
		for(Module child : children)
		{
			if(child != null) child.update(globals);
		}
	}
	
	/**
	 * 
	 */
	public void drawMe(Graphics g) 
	{
		Color c = null;
		
		if(bgColor != null)
		{
			c = g.getColor();
			g.setColor(bgColor);
			g.fillRect(0, 0, getWidth(), getHeight());
		}
		
		drawChildren(g);
		
		if(c != null) g.setColor(c);
	}
	
	/**
	 * 
	 */
	public void mouseMoved(MouseEvent e)
	{
		// Find module that is within this position
		Module mod = getNthChildrenAt(runner.getMouseX(), runner.getMouseY(), 1, false);
		
		// Case 1: no modules are currently moused over
		if(mousedOver == null)
		{
			mousedOver = mod;
		}
		
		// Case 2: new moused over module is not the same as the current moused over module.
		// Case 3: OR no module moused over.
		else if(!mousedOver.equals(mod))
		{
			// Inform the old module of this mouse moved event and let it know it is no longer of focus.
			mousedOver.mouseMoved(e);
			mousedOver = mod;
		}

		// Inform the current moused over module (if any) of this event
		if(mousedOver != null) mousedOver.mouseMoved(e);
	}
	
	/*******************************************************************
	 * 
	 * METHODS
	 * 
	 *******************************************************************/
	
	/**
	 * 
	 */
	public final GameRunner getRunner()
	{
		return runner;
	}
	
	/**
	 * 
	 * @param g
	 */
	public void drawChildren(Graphics g)
	{
		for(Module child : children)
		{
			if(child != null) child.drawMe(g);
		}
	}
	
	/**
	 * 
	 * @param c
	 */
	public void setBGColor(Color c)
	{
		bgColor = c;
	}
}
